package com.ms.game.bank.db;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.ms.game.common.OSType;
import com.ms.game.db.dto.TransactionsDetailsUtil;
import com.ms.game.purchase.bin.PurchaseType;
import com.ms.game.rebate.RebateType;
import com.ms.game.rebate.RebateUtil;
import com.ms.player.Player;
import com.ms.player.PlayerAttributeType;
import com.ms.player.UpdateAttribute;
import com.ms.player.UpdateAttributeList;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.account.db.AccountModel;
import com.xpec.c4.db.basic.BasicGameDAO;
import com.xpec.c4.db.hibernate.manager.HibernateLogManager;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.db.model.PlayerModel;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.datamanager.agent.money.ChangeCoin;
import com.xpec.c4.game.datamanager.agent.money.ChangeCoinLister;
import com.xpec.c4.game.datamanager.agent.money.CoinType;
import com.xpec.c4.game.datamanager.agent.money.MoneyChangeParam;
import com.xpec.c4.game.datamanager.agent.money.MoneyChangeWay;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GameUtility;

public class BankDAO extends BasicGameDAO {
	/** log4j object */
	private static final Log log = LogFactory.getLog(BasicGameDAO.class);
	
	private static final long MONTHLY_FEE_DAY = 30*24*60*60*1000l;
	
	/**
	 * 更新修改銀行密碼<br>
	 * 
	 * @param guid
	 * @return
	 */
	public static boolean UpdateBankPassword(GUID accountID, String password) {
		
		Session session = HibernateManager.txBegin();
		
		Query query = session
				.createQuery("update AccountModel set bankPassword = :bankPassword where accountId = :accountId");
		query.setParameter("accountId", accountID.toString());
		query.setParameter("bankPassword", password);
		query.executeUpdate();
		
		return HibernateManager.txCommitFailRollbackClearSession(); 
	}
	
	/**
	 * 更新修改銀行金錢(此函式只處理保險箱轉移金幣至玩家goldcoin)<br>
	 * 
	 * @param guid
	 * @return
	 */
	public static void UpdateBankMoney(MoneyChangeParam param, PlayerModel model,
			UpdateAttributeList updateAttrList) {
		
		// 沒有給執行參數
		if (param == null) {
			return;
		}

		// 沒有玩家資料
		if (model == null) {
			param.setResult(ErrorCode.INVALID_PARAM);
			return;
		}

		// 上一個動作沒有成功就不給做
		if (param.getResult() != ErrorCode.SUCCESS) {
			return;
		}

		// 處理所有的幣別
		ChangeCoinLister coinLister = param.getChangeCoinLister();
		ChangeCoin coin = coinLister.getCoinMap().get(CoinType.GOLD);
		BigDecimal bankGoldCoin = BigDecimal.ZERO;
		
		//提領銀行c幣
		if(param.getChangeWay() == MoneyChangeWay.ADD_BANK_GET_CASH){
			bankGoldCoin = Helper.mul(coin.getChangeValue(), new BigDecimal(-1));
			param.setGoldCoin(model.getGoldCoin().add(coin.getChangeValue()));
		}
		//抽傭
		else if(param.getChangeWay() == MoneyChangeWay.SUB_BANK_DEPOSIT){
			bankGoldCoin = coin.getChangeValue();
			param.setGoldCoin(model.getGoldCoin().add(coin.getChangeValue()));
		}
		

		param.setBankGoldCoin(model.getBankGoldCoin());
		
		if (updateAttrList == null) 
		{
			Session session = HibernateManager.txBegin();
			Query query = session
					.createQuery("update PlayerModel set bankGoldCoin = :bankGoldCoin where guid = :guid");
			query.setParameter("guid", param.getPlayerId().toString());
			query.setParameter("bankGoldCoin", Helper.add(model.getBankGoldCoin(), bankGoldCoin));
			query.executeUpdate();

			boolean res = HibernateManager.txCommitFailRollbackClearSession();
			if (!res) {
				param.setResult(ErrorCode.DB_OPERATION_FAIL);
				return;
			}
		}

		if (updateAttrList != null) 
		{
			updateAttrList.addUpdateValue(PlayerAttributeType.BANK_GOLD_COIN, Helper.add(model.getBankGoldCoin() ,bankGoldCoin));
		}

		param.setResult(ErrorCode.SUCCESS);
	}
	
	/**
	 * 更新修改銀行金錢<br>
	 * 
	 * @param playerID
	 * @param gold
	 *            要加到銀行保險箱中的金額, 要增加給正值, 要減少給負值.
	 * @return 成功回傳保險箱更新後的金額<br>
	 *         失敗回傳 -1
	 */
	public static BigDecimal UpdateBankMoney(GUID playerID, BigDecimal gold) {

		Session session = HibernateManager.txBegin();
		
		Query query = session.createQuery("select bankGoldCoin from PlayerModel where guid = :guid");
		query.setParameter("guid", playerID.toString());
		BigDecimal bankGoldCoin = (BigDecimal) query.uniqueResult();

		BigDecimal afterGold = Helper.add(bankGoldCoin, gold);
		
		query = session.createQuery("update PlayerModel set bankGoldCoin = :bankGoldCoin where guid = :guid");
		query.setParameter("guid", playerID.toString());
		query.setParameter("bankGoldCoin", afterGold);
		query.executeUpdate();

		boolean res = HibernateManager.txCommitFailRollbackClearSession(); 
		if(res){
			return afterGold;
		}else{
			return new BigDecimal(-1);
		}
	}
	
	/**
	 * 更新玩家點數<br>
	 * 
	 * @param guid
	 * @return
	 */
	@SuppressWarnings("finally")
	public static boolean UpdatePlayerVip(GUID playerID, int vip, long vipExp , boolean isFirstChange) {
		try{
			String queryStr = (isFirstChange)? "update PlayerModel set vipExp = :vipExp, vip = :vip , storeRewardContinuousLoginCount = 1 where guid = :guid" :
				"update PlayerModel set vipExp = :vipExp, vip = :vip where guid = :guid" ;
			Session session = HibernateManager.txBegin();
			
			Query query = session.createQuery(queryStr);
			query.setParameter("guid", playerID.toString());
			query.setParameter("vip", vip);
			query.setParameter("vipExp", vipExp);
			query.executeUpdate();
				
		}finally{
			return HibernateManager.txCommitFailRollbackClearSession(); 
		}
	}
	
	/**
	 * 更新玩家點數<br>
	 * 
	 * @param guid
	 * @return
	 */
	@SuppressWarnings("finally")
	public static boolean UpdateAccountCashPoint(GUID accountID, long cash , boolean isFirstChange) {
		try{
			GameUtility.writeCashUpdateConsoleLog("BankDAO.UpdateAccountCashPoint()", accountID.toString(), 0, 0, cash);
			
			String queryStr = (isFirstChange)? "update AccountModel set cash = :cash , haveFirstStoreCharacter = true where accountId = :accountId":
				"update AccountModel set cash = :cash where accountId = :accountId";
			Session session = HibernateManager.txBegin();
			Query query = session
				.createQuery(queryStr);
			query.setParameter("accountId", accountID.toString());
			query.setParameter("cash", cash);
			query.executeUpdate();
				
		}finally{
			return HibernateManager.txCommitFailRollbackClearSession(); 
		}
	}
	
	@SuppressWarnings("finally")
	public static boolean updateAccountFirstChangeStatus(GUID accountID, boolean isFirstChange){
		try {
			String queryStr = "update AccountModel set haveFirstStoreCharacter = true where accountId = :accountId";
			Session session = HibernateManager.txBegin();
			Query query = session.createQuery(queryStr);
			query.setParameter("accountId", accountID.toString());
			query.executeUpdate();
		}
		finally {
			return HibernateManager.txCommitFailRollbackClearSession();
		}
	}
	
	/**
	 * 更新玩家點數跟紅利<br>
	 * 
	 * @param guid
	 * @return
	 */
	@SuppressWarnings("finally")
	public static boolean UpdateAccountCashAndBonus(GUID accountID, long cash, BigDecimal bonus) {
		try{
			GameUtility.writeCashUpdateConsoleLog("BankDAO.UpdateAccountCashAndBonus()", accountID.toString(), 0, 0, cash);
			
			String queryStr = "update AccountModel set cash = :cash, bonus =:bonus where accountId = :accountId";

			Session session = HibernateManager.txBegin();
			Query query = session
				.createQuery(queryStr);
			query.setParameter("accountId", accountID.toString());
			query.setParameter("cash", cash);
			query.setParameter("bonus", bonus);
			query.executeUpdate();
				
		}finally{
			return HibernateManager.txCommitFailRollbackClearSession(); 
		}
	}
	
	/**
	 * 更新玩家點數<br>
	 * 
	 * @param guid
	 * @return Object[]<br>
	 *         [0] long, account cash<br>
	 *         [1] boolean, is Have First Store Character
	 */
	public static Object[] loadAccountCashPoint(GUID accountID) {
		AccountModel model = null;
		try {
			Session session = HibernateManager.getSession();
			Query query = session.createQuery("from AccountModel where accountId = :accountId");
			query.setParameter("accountId", accountID.toString());
			model = (AccountModel) query.uniqueResult();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("load account model " + accountID + " fail.", e);
			}
			return null;
		} finally {
			HibernateManager.endSession();
		}

		if (model == null) {
			return null;
		}

		if (log.isInfoEnabled()) {
			log.info("loadAccountCashPoint load account " + accountID + " cash " + model.getCash());
		}

		Object[] data = new Object[2];
		data[0] = model.getCash();
		data[1] = model.isHaveFirstStoreCharacter();

		return data;
	}
	
	/**
	 * 更新玩家包月時間<br>
	 * 
	 * @param guid
	 * @return
	 */
	public static void UpdatePlayerMonthlyFee(MoneyChangeParam param, PlayerModel model,
			UpdateAttributeList updateAttrList) {

		// 沒有給執行參數
		if (param == null) {
			return;
		}

		// 沒有玩家資料
		if (model == null) {
			param.setResult(ErrorCode.INVALID_PARAM);
			return;
		}

		// 上一個動作沒有成功就不給做
		if (param.getResult() != ErrorCode.SUCCESS) {
			return;
		}
		
		Date monthlyFeeEndTime = null;
		if(model.getMonthlyFeeEndTime()!=null){
			if(model.getMonthlyFeeEndTime().getTime() < TimeSys.currentTimeMills()){
				monthlyFeeEndTime = GameUtility.longToDate(TimeSys.currentTimeMills() + MONTHLY_FEE_DAY);
				model.setMonthlyFeeEndTime(monthlyFeeEndTime);
				model.setMonthlyAwardTime(GameUtility.longToDate(TimeSys.currentTimeMills()));
			}else{
				monthlyFeeEndTime = GameUtility.longToDate(model.getMonthlyFeeEndTime().getTime() + MONTHLY_FEE_DAY);
				model.setMonthlyFeeEndTime(monthlyFeeEndTime);
			}
		}else{
			//初始化
			monthlyFeeEndTime = GameUtility.longToDate(TimeSys.currentTimeMills() + MONTHLY_FEE_DAY);
			model.setMonthlyFeeEndTime(monthlyFeeEndTime);
			model.setMonthlyAwardTime(GameUtility.longToDate(TimeSys.currentTimeMills()));
		}

		if (updateAttrList == null) {
			Session session = HibernateManager.txBegin();
			Query query = session.createQuery("update PlayerModel set monthlyFeeEndTime = :monthlyFeeEndTime "
					+ ", monthlyAwardTime = :monthlyAwardTime " + "where guid = :guid");
			query.setParameter("guid", param.getPlayerId().toString());
			query.setParameter("monthlyFeeEndTime", model.getMonthlyFeeEndTime());
			query.setParameter("monthlyAwardTime", model.getMonthlyAwardTime());
			query.executeUpdate();

			boolean res = HibernateManager.txCommitFailRollbackClearSession();
			if (!res) {
				param.setResult(ErrorCode.DB_OPERATION_FAIL);
				return;
			}
		}
		
		if (updateAttrList != null) {
			updateAttrList.addUpdateValue(PlayerAttributeType.MONTHLY_FEE_END_TIME, model.getMonthlyFeeEndTime()
					.getTime());
			updateAttrList.addUpdateValue(PlayerAttributeType.MONTHLY_AWARD_TIME, model.getMonthlyAwardTime()
					.getTime());
		}
		param.setResult(ErrorCode.SUCCESS);;
	}
	

	public static boolean updateFeeInfo(Player player, int buyFeeType) {

		String guid = player.getGuid().toString();
		
		Date buyFeeAwardDate = null;
		Date buyFeeEndDate = null;
		int buyFeePayStage = 0;
			
		long feeEndTime = 0l;
		if (buyFeeType == PurchaseType.MONTH) {
			feeEndTime = player.getMonthlyFeeEndTime();
			buyFeeAwardDate = new Date(player.getMonthlyAwardTime());
			buyFeePayStage = player.getMonthlyFeePayStage();
		} else if (buyFeeType == PurchaseType.WEEK) {
			feeEndTime = player.getWeeklyFeeEndTime();
			buyFeeAwardDate = new Date(player.getWeeklyAwardTime());
			buyFeePayStage = player.getWeeklyFeePayStage();

		} else {
			return false;
		}
		
		if (feeEndTime >= GameUtility.getEventLastFlushTime(RebateUtil.getBuyFeeFlushTimeFormat(buyFeeType)).getTime()) {
			// 算新的結束時間
			Date newEndDate = new Date(feeEndTime);
			newEndDate.setDate(newEndDate.getDate() + RebateUtil.getBuyFeeDays(buyFeeType));
			buyFeeEndDate = newEndDate;
		} else {
			buyFeeAwardDate = GameUtility.getEventLastFlushTime(RebateUtil.getBuyFeeFlushTimeFormat(buyFeeType));
			buyFeeEndDate = getWeeklyDailyAwardEndDate(buyFeeType);
			buyFeePayStage = 0;		
		}
		
		Session session = HibernateManager.txBegin();
		Query query = null;
		if (buyFeeType == PurchaseType.MONTH) {
			query = session.createQuery("update PlayerModel set monthlyFeeEndTime = :monthlyFeeEndTime , monthlyAwardTime = :monthlyAwardTime , monthlyFeePayStage = :monthlyFeePayStage where guid = :guid");
			query.setParameter("guid", guid);
			query.setParameter("monthlyFeeEndTime", buyFeeEndDate);
			query.setParameter("monthlyAwardTime", buyFeeAwardDate);
			query.setParameter("monthlyFeePayStage", buyFeePayStage);
			query.executeUpdate();
		} else if (buyFeeType == PurchaseType.WEEK) {
			query = session.createQuery("update PlayerModel set weeklyFeeEndTime = :weeklyFeeEndTime , weeklyAwardTime = :weeklyAwardTime , weeklyFeePayStage = :weeklyFeePayStage where guid = :guid");
			query.setParameter("guid", guid);
			query.setParameter("weeklyFeeEndTime", buyFeeEndDate);
			query.setParameter("weeklyAwardTime", buyFeeAwardDate);
			query.setParameter("weeklyFeePayStage", buyFeePayStage);
			query.executeUpdate();
		}
		boolean res = HibernateManager.txCommitFailRollbackClearSession();	
		
		
		if (res) {
			if (buyFeeType == PurchaseType.MONTH) {
				player.setMonthlyAwardTime(buyFeeAwardDate.getTime());
				player.setMonthlyFeeEndTime(buyFeeEndDate.getTime());
			} else if (buyFeeType == PurchaseType.WEEK) {
				player.setWeeklyAwardTime(buyFeeAwardDate.getTime());
				player.setWeeklyFeeEndTime(buyFeeEndDate.getTime());
			}
		}
		
		return res;
	}
	
	
	
	public static boolean UpdatePlayerMonthlyFee(PlayerModel model){
//		Date monthlyFeeEndTime = null;
//		if(model.getMonthlyFeeEndTime()!=null){
//			if(model.getMonthlyFeeEndTime().getTime() < TimeSys.currentTimeMills()){
//				monthlyFeeEndTime = GameUtility.longToDate(TimeSys.currentTimeMills() + MONTHLY_FEE_DAY);
//				model.setMonthlyFeeEndTime(monthlyFeeEndTime);
//				model.setMonthlyAwardTime(GameUtility.longToDate(TimeSys.currentTimeMills()));
//			}else{
//				monthlyFeeEndTime = GameUtility.longToDate(model.getMonthlyFeeEndTime().getTime() + MONTHLY_FEE_DAY);
//				model.setMonthlyFeeEndTime(monthlyFeeEndTime);
//			}
//		}else{
//			//初始化
//			monthlyFeeEndTime = GameUtility.longToDate(TimeSys.currentTimeMills() + MONTHLY_FEE_DAY);
//			model.setMonthlyFeeEndTime(monthlyFeeEndTime);
//			model.setMonthlyAwardTime(GameUtility.longToDate(TimeSys.currentTimeMills()));
//		}

		Date monthlyAwardTime = null;
		Date monthlyFeeEndTime = null;
		int monthlyFeePayStage = 0;
		if(model.getMonthlyFeeEndTime().getTime() >= GameUtility.getEventLastFlushTime(RebateUtil.getMonthlyFeeFlushTimeFormat()).getTime()){
			//如果結束時間>=今天 ， 就代表還有包月
			monthlyAwardTime =	model.getMonthlyAwardTime();
			model.getMonthlyFeeEndTime().setDate(model.getMonthlyFeeEndTime().getDate() + RebateUtil.getMonthlyFeeDays());
			monthlyFeeEndTime = model.getMonthlyFeeEndTime();
			monthlyFeePayStage = model.getMonthlyFeePayStage();
		} else {
			// 如果結束時間<今天 (包月過期要重新開始)
			monthlyAwardTime = GameUtility.getEventLastFlushTime(RebateUtil.getMonthlyFeeFlushTimeFormat());
			monthlyFeeEndTime = getMonthDailyAwardEndDate();
			monthlyFeePayStage = 0;
		}

		Session session = HibernateManager.txBegin();
		Query query = session.createQuery("update PlayerModel set monthlyFeeEndTime = :monthlyFeeEndTime , monthlyAwardTime = :monthlyAwardTime , monthlyFeePayStage = :monthlyFeePayStage where guid = :guid");
		query.setParameter("guid", model.getGuid().toString());
		query.setParameter("monthlyFeeEndTime", monthlyFeeEndTime);
		query.setParameter("monthlyAwardTime", monthlyAwardTime);
		query.setParameter("monthlyFeePayStage", monthlyFeePayStage);
		query.executeUpdate();

		boolean res = HibernateManager.txCommitFailRollbackClearSession();	
		
		if(res){
			model.setMonthlyAwardTime(monthlyAwardTime);
			model.setMonthlyFeeEndTime(monthlyFeeEndTime);
		}
		return res;
	}

	public static boolean UpdatePlayerWeeklyFee(PlayerModel model, int buyFeeType){

		Date buyAwardTime = null;
		Date buyFeeEndTime = null;
		int buyFeePayStage = 0;
		if(model.getWeeklyFeeEndTime().getTime() >= GameUtility.getEventLastFlushTime(RebateUtil.getBuyFeeFlushTimeFormat(buyFeeType)).getTime()){
			//如果結束時間>=今天 ， 就代表還有包月
			buyAwardTime =	model.getWeeklyAwardTime();
			model.getWeeklyFeeEndTime().setDate(model.getWeeklyFeeEndTime().getDate() + RebateUtil.getBuyFeeDays(buyFeeType));
			buyFeeEndTime = model.getWeeklyFeeEndTime();
			buyFeePayStage = model.getWeeklyFeePayStage();
		}else{
			//如果結束時間<今天 (包月過期要重新開始)
			buyAwardTime = GameUtility.getEventLastFlushTime(RebateUtil.getBuyFeeFlushTimeFormat(buyFeeType));
			buyFeeEndTime = getWeeklyDailyAwardEndDate(buyFeeType);
			buyFeePayStage = 0;
		}

		Session session = HibernateManager.txBegin();
		Query query = null;
		if (buyFeeType == PurchaseType.MONTH) {
			query = session.createQuery("update PlayerModel set monthlyFeeEndTime = :monthlyFeeEndTime , monthlyAwardTime = :monthlyAwardTime , monthlyFeePayStage = :monthlyFeePayStage where guid = :guid");
			query.setParameter("guid", model.getGuid().toString());
			query.setParameter("monthlyFeeEndTime", buyFeeEndTime);
			query.setParameter("monthlyAwardTime", buyAwardTime);
			query.setParameter("monthlyFeePayStage", buyFeePayStage);
			query.executeUpdate();
		} else if (buyFeeType == PurchaseType.WEEK) {
			query = session.createQuery("update PlayerModel set weeklyFeeEndTime = :weeklyFeeEndTime , weeklyAwardTime = :weeklyAwardTime , weeklyFeePayStage = :weeklyFeePayStage where guid = :guid");
			query.setParameter("guid", model.getGuid().toString());
			query.setParameter("weeklyFeeEndTime", buyFeeEndTime);
			query.setParameter("weeklyAwardTime", buyAwardTime);
			query.setParameter("weeklyFeePayStage", buyFeePayStage);
			query.executeUpdate();
		}

		boolean res = HibernateManager.txCommitFailRollbackClearSession();	
		
		if(res){
			model.setWeeklyAwardTime(buyAwardTime);
			model.setWeeklyFeeEndTime(buyFeeEndTime);
		}
		return res;
	}
	
	
	/**
	 * 更新領取包月獎勵時間<br>
	 * 
	 * @param guid
	 * @return
	 */
	public static boolean UpdatePlayerMonthlyFee(GUID playerID, long dateTime,int stage) {
		Session session = HibernateManager.txBegin();
		Query query = session
				.createQuery("update PlayerModel set monthlyAwardTime = :monthlyAwardTime, monthlyFeePayStage = :stage where guid = :guid");
		query.setParameter("guid", playerID.toString());
		query.setParameter("stage", stage);
		query.setParameter("monthlyAwardTime", GameUtility.longToDate(dateTime));
		query.executeUpdate();
				
		return HibernateManager.txCommitFailRollbackClearSession(); 
	}

	/**
	 * 更新領取包週獎勵時間<br>
	 * 
	 * @param guid
	 * @return
	 */
	public static boolean UpdatePlayerWeeklyFee(GUID playerID, long dateTime,int stage, RebateType rebateType) {
		Session session = HibernateManager.txBegin();
		Query query = null;
		if (rebateType == RebateType.MONTHLY_FEE_DAILY_PAY) {
			query = session
					.createQuery("update PlayerModel set monthlyAwardTime = :monthlyAwardTime, monthlyFeePayStage = :stage where guid = :guid");
			query.setParameter("guid", playerID.toString());
			query.setParameter("stage", stage);
			query.setParameter("monthlyAwardTime", GameUtility.longToDate(dateTime));
			query.executeUpdate();
		} else 	if (rebateType == RebateType.WEEKLY_FEE_DAILY_PAY) {
			query = session
					.createQuery("update PlayerModel set weeklyAwardTime = :weeklyAwardTime, weeklyFeePayStage = :stage where guid = :guid");
			query.setParameter("guid", playerID.toString());
			query.setParameter("stage", stage);
			query.setParameter("weeklyAwardTime", GameUtility.longToDate(dateTime));
			query.executeUpdate();
		}
				
		return HibernateManager.txCommitFailRollbackClearSession(); 
	}
	
	/**
	 * 更新玩家保險箱金幣 by 領紅包
	 * @return
	 */
	public static boolean UpdatePlayerMoneyByDrawHonbo
	(
			MoneyChangeParam param, 
			PlayerModel model,
			UpdateAttributeList updateAttrList
	)
	{
		Session session = HibernateManager.txBegin();
		
		ArrayList<UpdateAttribute> attribes = updateAttrList.getUpdateList();
		
		//檢查是否有不是c幣的請求
		UpdateAttribute attr = null;
		for(UpdateAttribute tt:attribes)
		{
			if(attr.getId() == CoinType.GOLD.getId())
			{
				attr = tt;
				break;
			}
		}
		
		//更新玩家bankgoldcoin
		Query query = session
				.createQuery("update PlayerModel set monthlyAwardTime = :monthlyAwardTime "
						+ "where guid = :guid");
		query.setParameter("guid", model.getGuid());
		query.setParameter("bankGoldCoin", model.getBankGoldCoin().add(new BigDecimal( attr.getValueLong())));
		query.executeUpdate();
		return HibernateManager.txCommitFailRollbackClearSession(); 
	
	}
	
	private static Date getMonthDailyAwardEndDate() {
		Date date = GameUtility.getEventLastFlushTime(RebateUtil.getMonthlyFeeFlushTimeFormat());
		// 當日不算，往後算三十天
		date.setDate(date.getDate() + RebateUtil.getMonthlyFeeDays());
		return date;
	}

	private static Date getWeeklyDailyAwardEndDate(int buyFeeType) {
		Date date = GameUtility.getEventLastFlushTime(RebateUtil.getBuyFeeFlushTimeFormat(buyFeeType));
		// 當日不算，往後算三十天
		date.setDate(date.getDate() + RebateUtil.getBuyFeeDays(buyFeeType));
		return date;
	}
	
	public static ArrayList<BankLogModel> getBankBookDetail(String playerId, long from, long end){
		from = TransactionsDetailsUtil.checkTransactionDateRange(from);
		end = TransactionsDetailsUtil.checkTransactionDateRange(end);
		if(from == end){
			return null;
		}
		Session session = HibernateLogManager.getSession();
		SQLQuery query = session.createSQLQuery("select * from bank_log  where playerId = :playerId and "
				+ "(gameTime > :end and gameTime < :from) order by gameTime desc");
		query.addEntity(BankLogModel.class);
		query.setParameter("playerId", playerId);
		query.setParameter("end", end);
		query.setParameter("from", from);
		ArrayList<BankLogModel> bankLogModels = (ArrayList<BankLogModel>)query.list();
		HibernateLogManager.endSession();
		return bankLogModels;
	}
	
	/**
	 * 2017-09-11 因為沒串到包週部分，所以暫時改用這個方法
	 * 之後修掉再改回來
	 * @param playerId
	 * @param osType
	 * @param from
	 * @param end
	 * @return
	 */
	public static ArrayList<BankLogModel> getBankBookDetail(String playerId, OSType osType, long from, long end) {
		from = TransactionsDetailsUtil.checkTransactionDateRange(from);
		end = TransactionsDetailsUtil.checkTransactionDateRange(end);
		if(from == end){
			return null;
		}
		Session session = HibernateLogManager.getSession();
		String queryStr = "";
		if(osType != null && osType == OSType.IOS){
			queryStr = "select * from bank_log  where playerId = :playerId and changeWay != 36 and changeWay != 37 and "
					+ "(gameTime > :end and gameTime < :from) order by gameTime desc;";
		}else {
			queryStr = "select * from bank_log  where playerId = :playerId and "
					+ "(gameTime > :end and gameTime < :from) order by gameTime desc;";
		}
		
		SQLQuery query = session.createSQLQuery(queryStr);
		query.addEntity(BankLogModel.class);
		query.setParameter("playerId", playerId);
		query.setParameter("end", end);
		query.setParameter("from", from);
		ArrayList<BankLogModel> bankLogModels = (ArrayList<BankLogModel>)query.list();
		HibernateLogManager.endSession();
		return bankLogModels;
	}
}
